<?php
namespace app\index\taglib;
use think\template\TagLib;

class TH extends TagLib
{
    protected $tags = [
        'menu'      => ['attr' => 'name', 'close' => 1], //0闭合标签 ，1不闭合（默认）
        'hot'       => ['attr' => 'catid,num,name,table,field', 'close' => 1],
        'recommend' => ['attr' => 'catid,num,name,table,field', 'close' => 1],
        'list'      => ['attr' => 'catid,num,name,table,field', 'close' => 1],
        'data'      =>['attr'=>'catid,pid,name,table,ismenu','close'=>1],
        'danye'      =>['attr'=>'catid,name,table','close'=>1],

    ];

    public function tagMenu($tag, $content) {

      $php = <<<php
<?php
        if(!cache('menu')) {
            \$categoryArray = think\Db::name('category')->where('ismenu',1)->order('listorder')->select();
            cache('menu',app\manage\model\Menu::tree(\$categoryArray));
        }
      
        \$__LIST__ = cache('menu');
?>
php;
        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagHot($tag, $content) {
        $table = $tag['table'];
        $field = $tag['field']; //可为空，则查询所有字段
        if(empty($tag['catid']) || $tag['catid']=='0'){
            $where = '';
        }else{
            $where = "catid=" . $tag['catid'];
        }
        if(empty($tag['num']) || $tag['num']=='0'){
            $num = 8;
        }else{
            $num = $tag['num'];
        }
        /*$cache = 'hot_' . $tag['catid'];*/

        $php = <<<php
<?php
        \$hot = think\Db::name('$table')->where("$where")->field('$field')->limit('$num')->order('listorder desc')->select(); 
        \$__LIST__ = \$hot;
?>
php;

        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagRecommend($tag, $content) {
        $table = $tag['table'];
        $field = $tag['field'];
        if(empty($tag['catid']) || $tag['catid']=='0'){
            $where = 'recommend=1';
        }else{
            $where = "recommend=1 and catid=" . $tag['catid'];
        }
        if(empty($tag['num']) || $tag['num']=='0'){
            $num = 3;
        }else{
            $num = $tag['num'];
        }
//        $cache = 'recommend_' . $tag['catid'];

        $php = <<<php
<?php
        \$recommend = think\Db::name('$table')->where("$where")->field('$field')->limit('$num')->order('listorder desc')->select();
   
        \$__LIST__ = \$recommend;
?>
php;
        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagList($tag, $content) {
        $table = $tag['table'];
        $field = $tag['field'];
        $pages= $tag['pages'];
        //首页cid=0,num>0 ; 分类页cid>0,num=0
        if(empty($tag['catid']) || $tag['catid']=='0'){
            $num = $tag['num'];
            $php = <<<php
<?php
            \$list = think\Db::name('$table')->field('$field')->limit("$num")->order('listorder desc')->paginate($pages);
            \$__LIST__ = \$list;
?>
php;
        }else{
            $num = 0;
            $where = "catid=" . $tag['catid'];
            $php = <<<php
<?php
           \$list = think\Db::name('$table')->where("$where")->field('$field')->limit("$num")->order('listorder desc')->paginate($pages);
           \$__LIST__ = \$list;
?>
php;
        }

        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }
    public function tagData($tag, $content){

        $table = $tag['table'];
        $field = $tag['field'];
        $where = "ismenu=1 and parentid=" . $tag['pid'];
//        $cache = 'data_' . $tag['catid'];
        $php = <<<php
<?php
       
            \$data = think\Db::name('$table')->where('$where')->field('$field')->select();
            \$__LIST__ = \$data;

?>
php;
        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagDanye($tag, $content){

        $table = $tag['table'];
        $where = "catid=" . $tag['catid'];
        $php = <<<php
<?php
       
            \$danye = think\Db::name('$table')->where('$where')->select();
            \$__LIST__ = \$danye;

?>
php;
        $name = $tag['name'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $name . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

}
